/**
 * [33] 搜索旋转排序数组
 *
 * @format
 * @lc app=leetcode.cn id=33 lang=javascript
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function (nums, target) {
    let [left, right, mid] = [0, nums.length - 1, 0]
    let [A, B, C] = [true, true, true]
    let count = 0
    while (left <= right && count < nums.length / 2 + 1) {
        if (nums[mid] === target) return mid
        // target > nums[0]    1 1 1 1 0 0 0 0 A
        // nums[mid] > nums[0] 1 1 0 0 1 1 0 0 B
        // nums[mid] > target  1 0 1 0 1 0 1 0 C
        //                     < > - < > - < > Y
        // A·B·C+A·B`·C`+A`·B`·C  => right = mid
        // 卡诺图
        // A\BC 00 01 11 10
        //    0  0  1  0  0
        //    1  1  0  1  0
        // A·B·C`+A`·B·C+A`·B`·C` => left = mid
        // 卡诺图
        // A\BC 00 01 11 10
        //    0  1  0  1  0
        //    1  0  0  0  1
        A = target >= nums[0]
        B = nums[mid] >= nums[0]
        C = nums[mid] > target
        if ((A && B && C) || (A && !B && !C) || (!A && !B && C)) {
            right = mid
            mid = Math.floor((left + right) / 2)
        } else {
            left = mid
            mid = Math.floor((left + right + 1) / 2)
        }
        count++
    }
    return -1
}
// debugger
// const nums = [1]
// const target = 1
// const result = search(nums, target)
// console.log(result)
// @lc code=end
